package cn.jwm.permission.controller;

import cn.jwm.annotation.JwmPermission;
import cn.jwm.dto.RolePermissionDTO;
import cn.jwm.permission.domain.Role;
import cn.jwm.permission.query.RoleQuery;
import cn.jwm.permission.service.IRoleService;
import cn.jwm.permission.service.impl.RoleServiceImpl;
import cn.jwm.utils.AjaxResult;
import cn.jwm.utils.PageList;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Objects;

/**
 * @author Administrator
 */
@JwmPermission(name = "角色管理")
@RestController
@RequestMapping("/role")
public class RoleController {
    @Autowired
    private IRoleService roleService;

    @JwmPermission(name = "增")
    @PutMapping
    public AjaxResult addOrUpdata(@RequestBody  Role role){
        try {
            if (Objects.isNull(role)){
                roleService.add(role);
            }else {
                roleService.upData(role);
            }
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("保存失败!"+e.getMessage());
        }
    }

    @JwmPermission(name = "按id删除")
    @DeleteMapping
    public AjaxResult deleteById(@PathVariable("id")Long id){
        try {
            roleService.deleteById(id);
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("删除失败!"+e.getMessage());
        }
    }

    @JwmPermission(name = "批量删除")
    @PatchMapping
    public AjaxResult batchDelete(@RequestBody List<Long> ids){
        try {
            roleService.patchDelete(ids);
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("删除失败"+e.getMessage());
        }
    }


    @JwmPermission(name = "按id查询")
    @GetMapping("/{id}")
    public AjaxResult getById(@PathVariable Long id){
        try {
            Role role = roleService.getById(id);
            return AjaxResult.me().setDataObj(role);
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("查询失败"+e.getMessage());
        }
    }

    @JwmPermission(name = "查询所有")
    @GetMapping
    public AjaxResult getAll(){
        try {
            List<Role> roles = roleService.getAll();
            return AjaxResult.me().setDataObj(roles);
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("查询失败!"+e.getMessage());
        }
    }

    @JwmPermission(name = "分页查询")
    @PostMapping
    public AjaxResult listPage(@RequestBody RoleQuery query){
        try {
            PageList<Role> pageList = roleService.page(query);
            return AjaxResult.me().setDataObj(pageList);
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("分页查询失败!"+e.getMessage());
        }
    }

    @JwmPermission(name = "设置权限")
    @PutMapping("/permission")
    public AjaxResult saveRolePermission(@RequestBody RolePermissionDTO dto){
        try {
            roleService.saveRolePermission(dto);
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("设置权限失败!"+e.getMessage());
        }
    }

    @JwmPermission(name = "设置角色权限")
    @GetMapping("/permissionsns/{roleId}")
    public AjaxResult getRolePermissionSns(@PathVariable("roleId")Long roleId){
        try {
            List<String> sns = roleService.getRolePermissionSns(roleId);
            return AjaxResult.me().setDataObj(sns);
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("获取角色的权限失败!"+e.getMessage());
        }
    }
}
